package com.meisiabc.app.service;

import com.alibaba.fastjson.JSONObject;
import com.meisiabc.app.repository.db.entity.FreeAudition;
import com.meisiabc.app.repository.db.entity.Student;
import com.meisiabc.app.repository.db.entity.WechatAppSession;
import com.meisiabc.app.repository.db.manager.FreeAuditionManager;
import com.meisiabc.app.repository.db.manager.StudentManager;
import com.meisiabc.app.repository.db.manager.WechatAppSessionManager;
import com.meisiabc.app.web.advice.ResultException;
import com.meisiabc.app.web.auth.Authentication;
import com.meisiabc.app.web.auth.JwtAuthHandler;
import com.meisiabc.support.wechat.WechatClient;
import com.meisiabc.support.wechat.WechatSession;
import com.meisiabc.utils.Verify;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

import static com.meisiabc.app.web.advice.ResponseCode.NOT_REGISTED;

@Slf4j
@Service
public class ProfileService {
    @Autowired
    private WechatAppSessionManager wechatAppSessionManager;
    @Autowired
    private StudentManager studentManager;
    @Autowired
    private FreeAuditionManager freeAuditionManager;
    @Autowired
    private WechatClient wechatClient;

    public JSONObject wechatLogin(String code) {
        WechatSession wechatSession = wechatClient.code2Session(code);
        Verify.notNull(wechatSession, "wechat code to session error.[code={}]", code);

        String openid = wechatSession.getOpenid();
        WechatAppSession wechatAppSession = wechatAppSessionManager.selectByOpenid(openid);
        if (wechatAppSession == null) {
            DateTime now = DateTime.now();
            wechatAppSessionManager.save(WechatAppSession.builder().openid(openid).createTime(now.toDate()).updateTime(now.toDate()).build());
            return new JSONObject().fluentPut("openid", openid);
        }

        if (wechatAppSession.getMobile() == null) {
            return new JSONObject().fluentPut("openid", openid);
        }

        String tokenValue = JwtAuthHandler.resolveAuthTokenValue(Authentication.builder().openid(openid).mobile(wechatAppSession.getMobile()).createTime(new Date().getTime()).build());

        return new JSONObject().fluentPut("openid", openid)
                .fluentPut("mobile", wechatAppSession.getMobile())
                .fluentPut("token", tokenValue);
    }

    public void assertRegisted(String mobile) {
        Student student = studentManager.selectByMobile(mobile);
        FreeAudition freeAudition = freeAuditionManager.selectLastOneByMobile(mobile);
        if (student == null && freeAudition == null) {
            throw new ResultException(NOT_REGISTED);
        }
    }

    public JSONObject smsLogin(String openid, String mobile) {
        WechatAppSession wechatAppSession = wechatAppSessionManager.selectByOpenid(openid);
        Verify.notNull(wechatAppSession, "sms login error,wechatAppSession is null..[openid={},mobile={}]", openid, mobile);

        wechatAppSessionManager.updateById(WechatAppSession.builder().id(wechatAppSession.getId()).mobile(mobile).updateTime(DateTime.now().toDate()).build());

        String tokenValue = JwtAuthHandler.resolveAuthTokenValue(Authentication.builder().openid(openid).mobile(mobile).createTime(new Date().getTime()).build());
        return new JSONObject().fluentPut("openid", openid)
                .fluentPut("mobile", mobile)
                .fluentPut("token", tokenValue);
    }

    public JSONObject modiftyMobile(String openid, String oldMobile, String newMobile) {
        studentManager.updateMobile(oldMobile, newMobile);
        return smsLogin(openid, newMobile);
    }
}
